package com.jd.org.controller;


import com.jd.basic.config.BusinessException;
import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.ExcelUtil;
import com.jd.basic.util.FastDfsUtil;
import com.jd.basic.util.PageList;
import com.jd.org.domain.Shop;
import com.jd.org.domain.ShopAuditLog;
import com.jd.org.query.ShopQuery;
import com.jd.org.service.IShopService;
import com.jd.system.dto.ShopDto;
import com.sun.deploy.util.JVMParameters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 接口类：店铺
 */
@RestController
@RequestMapping("/shop")
@Api(value = "店铺接口", description = "店铺接口")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 接口：查询单个对象
     *
     * @param id
     * @return
     */
    //http://localhost:8080/shop/1 + get
    @ApiOperation(value = "查询单个对象")
    @GetMapping("/{id}")
    public Shop loadOne(@PathVariable("id") Long id) {
        return shopService.loadById(id);
    }

    /**
     * 接口：查询所有对象
     *
     * @return
     */
    //http://localhost:8080/shop + 无 + get
    @ApiOperation(value = "查询所有对象")
    @GetMapping
    public List<Shop> loadAll() {
        return shopService.loadAll();
    }

    /**
     * 接口：分页查询
     *
     * @param query
     * @return
     */
    //http://localhost:8080/shop + {json对象} + post
    @ApiOperation(value = "分页查询")
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    //http://localhost:8080/shop/1 + remove
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            shopService.remove(id);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     * 接口：添加或修改
     *
     * @param shop
     * @return
     */
    @ApiOperation(value = "添加或修改")
    //http://localhost:8080/shop + {json对象} + put
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() == null) {//添加
                shopService.save(shop);
            } else {
                shopService.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }


    /**
     * 接口：批量删除
     *
     * @param
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult patchDelete(@RequestBody Long[] ids) {
        try {
            shopService.patchDelete(ids);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    @PostMapping("/settlement")
    @ApiOperation(value = "商家入驻", notes = "")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResult.me();//true + "操作成功"
        } catch (BusinessException e) {//业务异常捕获
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("入驻失败！" + e.getMessage());
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常,请稍后重试");
        }
    }


    /**
     * 接口：店铺审核通过
     *
     * @param
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审核通过")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult pass(@RequestBody ShopAuditLog log) {
        try {
            shopService.pass(log);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "审核失败");
        }
    }


    /**
     * 接口：店铺审核驳回
     *
     * @param
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审核驳回")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult reject(@RequestBody ShopAuditLog log) {
        try {
            shopService.reject(log);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "驳回失败");
        }
    }


    /**
     * 导出数据
     *
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) {

        List<Shop> shops = shopService.loadAll();
        //Shops:导出的数据
        //null:标题 - 一般无
        //“店铺信息” - sheet页的名称
        //Shop.class - 导出的类型
        //“shop.xlsx” - 导出文件名
        //response - 到处是的响应对象
        ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
    }

    /**
     * 导入数据:前端要上传
     * el-upload中name属性值="false"
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file) {
        //file - 要导入的文件 - 前端上传的文件
        //0,标题行数 - 没有
        //1,头函数 = 字段名
        //Shop.class = 导入时映射的对象
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        System.out.println("导入数据一共【" + shops.size() + "】行");
        shops.forEach(System.out::println);
    }

    @GetMapping("/echarts")
    public List<ShopDto> getShopState() {
        return shopService.getShopState();
    }
}